package cn.net.gemai.springboot_quartz.config;

import cn.net.gemai.springboot_quartz.jobs.ConsoleJob;
import cn.net.gemai.springboot_quartz.jobs.ConsoleJob2;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ConsoleJobConfig {

    @Bean
    public JobDetail jobDetail(){
        return JobBuilder.newJob(ConsoleJob.class)//ConsoleJob是业务类
                .withIdentity("ConsoleJob")//可以给该JobDetail起一个id
                //每个JobDetail内都有一个Map，包含了关联到这个Job的数据，在Job类中可以通过context获取
                .usingJobData("msg", "Hello Quartz")//关联键值对
                .storeDurably()//即使没有Trigger关联时，也不需要删除该JobDetail
                .build();
    }

    @Bean
    public Trigger trigger() {
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/2 * * * * ?");
        return TriggerBuilder.newTrigger()
                .forJob(jobDetail())//关联上述的JobDetail
                .withIdentity("quartzTaskService")//给Trigger起个名字
                .withSchedule(cronScheduleBuilder)
                .build();
    }

    @Bean
    public JobDetail jobDetail2(){
        return JobBuilder.newJob(ConsoleJob2.class)//ConsoleJob是业务类
                .withIdentity("ConsoleJob222")//可以给该JobDetail起一个id
                .storeDurably()
                .build();
    }

    @Bean
    public Trigger trigger2() {
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/3 * * * * ?");
        return TriggerBuilder.newTrigger()
                .forJob(jobDetail2())//关联上述的JobDetail
                .withIdentity("quartzTaskService22")//给Trigger起个名字
                .withSchedule(cronScheduleBuilder)
                .build();
    }
}
